package com.cellulam.framework.core;

import com.cellulam.framework.core.log.LogClient;
import com.cellulam.framework.core.rest.LogRestClient;
import com.cellulam.framework.core.rest.RestClient;
import com.cellulam.framework.core.shortlink.ShortLinkGenerator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CoreAutoConfiguration {

    @Value("${request.rest.timeout:3000}")
    private int timeout;

    @Value("${short.link.api.url:}")
    private String shortLinkApiUrl;

    @Value("${short.link.api.token:}")
    private String shortLinkApiToken;

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnClass(RestClient.class)
    public RestClient restClient() {
        return new RestClient(timeout);
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnBean({RestClient.class, LogClient.class})
    public LogRestClient logRestClient(LogClient logClient) {
        return new LogRestClient(logClient, timeout);
    }

    @Bean
    @ConditionalOnMissingBean
    public ShortLinkGenerator shortLinkGenerator(RestClient restClient) {
        return new ShortLinkGenerator(shortLinkApiUrl, shortLinkApiToken, restClient);
    }
}
